<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card>
        <el-row type="flex" justify="end" style="margin-bottom: 20px">
          <el-button size="mini" type="primary" style="float: right" @click="showDialog(1, '0')">添加权限</el-button></el-row>
        <el-table :data="tableData" border stripe row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <!-- {{ row }} -->
              <el-button v-if="row.pid === '0'" size="mini" type="success" @click="showDialog(2, row.id)">添加权限</el-button>
              <el-button size="mini" type="warning" @click="showEdit(row.id)">查看权限</el-button>
              <el-button size="mini" type="danger" @click="delRights(row.id)">删除权限</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 对话框 -->
      <el-dialog
        :title="title"
        :visible="dialogVisible"
        width="30%"
        @close="dialogVisible = false"
      >
        <!-- 表单内容 -->
        <el-form label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="hadleSubmit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { reqGetPermissionList, reqAddPermission, reqGetPermissionDetail, reqDelPermission, reqUpdatePermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      tableData: [], // 表格数据
      dialogVisible: false, // 控制对话框的布尔值默认是false
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      title: '新增权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取所有权限列表
    async getPermissionList() {
      const { data } = await reqGetPermissionList()
      this.tableData = tranListToTreeData(data, '0')
    },
    // 点击新增按钮
    showDialog(type, pid) {
      this.title = '新增权限'
      this.dialogVisible = true
      this.formData.type = type // 赋值type字段
      this.formData.pid = pid // 赋值pid字段
    },
    // 点击编辑按钮展示对话框并回显
    async showEdit(id) {
      this.title = '查看权限'
      this.dialogVisible = true
      const { data } = await reqGetPermissionDetail(id)
      this.formData = data
    },
    // 点击确定按钮新增/修改
    async hadleSubmit() {
      // 1. 预校验 省略 没有写规则 自己写
      // 2. 发送请求
      if (this.formData.id) {
        await reqUpdatePermission(this.formData)
      } else {
        await reqAddPermission(this.formData)
      }
      // 3. 关闭对话框
      this.dialogVisible = false
      // 4. 重新渲染
      this.getPermissionList()
      // 5. 表单清空
      this.formData = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 删除权限
    delRights(id) {
      // 1. 确认提示
      this.$confirm('此操作将永久删除该权限点, 是否继续?', '提示', {
        type: 'warning'
      }).then(async() => {
        // 2. 发请求
        await reqDelPermission(id)
        // 3. 重新渲染
        this.getPermissionList()
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    }
  }
}
</script>

<style>

</style>
